#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# BSD License
# Copyright (c) 2011, Wang Qiang
# All rights reserved.

"""
Test twisted.
Learn how to use twisted.
"""

from twisted.python import log
from twisted.python.logfile import DailyLogFile
from twisted.internet import reactor
from twisted.internet import defer
from twisted.web.client import getPage
import urllib2
import time

#log.startLogging(DailyLogFile.fromFullPath("/var/log/news_retrieval.log"))
#log.msg('Test the logging of twisted')

def download_ok(page):
    print "ok"


def download_failed(error):
    print "error"
    print error


def download_done(_):
    print "Stop"
    reactor.stop()


def main1():
    d1 = getPage("http://www.163.com")
    d1.addCallbacks(download_ok, download_failed)
    d2 = getPage("http://www.sina.com.cn")
    d2.addCallbacks(download_ok, download_failed)
    d3 = getPage("http://www.qq.com")
    d3.addCallbacks(download_ok, download_failed)
    d4 = getPage("http://www.hao123.com")
    d4.addCallbacks(download_ok, download_failed)
    dl = defer.DeferredList([d1, d2, d3, d4])
    dl.addCallback(download_done)
    reactor.run()


def main2():
    d1 = "http://www.163.com"
    html = urllib2.urlopen(d1).read()
    d2 = "http://www.sina.com.cn"
    html = urllib2.urlopen(d2).read()
    d3 = "http://www.qq.com"
    html = urllib2.urlopen(d3).read()
    d4 = "http://www.hao123.com"
    html = urllib2.urlopen(d4).read()


if __name__ == "__main__":
    ts = time.time()
    print "start..."
    main1()
    print "end..."
    t1 = time.time() - ts
    ts = time.time()
    print "start..."
    main2()
    print "end..."
    t2 = time.time() - ts
    print str(t1) + "  |  " + str(t2)
